package com.songlanyun.modules.schedule;

import com.songlanyun.common.enums.OpusConstant;
import com.songlanyun.common.utils.RedisKeys;
import com.songlanyun.common.utils.RedisUtils;
import com.songlanyun.modules.article.service.ArticleService;
import com.songlanyun.modules.interact.IInteractService;
import com.songlanyun.modules.opus.service.OpusService;
import com.songlanyun.modules.share.service.GoodsShareService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 定时任务
 * <p>
 * 检测过期订单，修改状态
 */
@Component
@Configuration
@EnableScheduling
@Lazy(false)
@Slf4j
public class BbsScheduleServiceImpl {

    @Resource
    private RedisUtils redisUtils;

    @Resource(name = "iInteractService")
    private IInteractService iInteractService;

    @Resource
    private GoodsShareService goodsShareService;

    /**
     * 作品浏览任务
     */
    @Scheduled(fixedDelay = 15 * 1000)
    public void browseOpusTask() {
        String key = RedisKeys.BbsKey.BROWSE_OPUS_KEY.getKey();
        BoundHashOperations<String, String, Object> result = getResult(key);
        iInteractService.browseSave(result);
    }

    /**
     * 作品点赞任务
     */
    @Scheduled(fixedDelay = 15 * 1000)
    public void likeOpusTask() {
        String key = RedisKeys.BbsKey.LIKE_OPUS_KEY.getKey();
        BoundHashOperations<String, String, Object> result = getResult(key);
        iInteractService.likeSave(result);
    }

    /**
     * 作品分享任务
     */
    @Scheduled(fixedDelay = 15 * 1000)
    public void shareOpusTask() {
        String key = RedisKeys.BbsKey.SHARE_OPUS_KEY.getKey();
        BoundHashOperations<String, String, Object> result = getResult(key);
        iInteractService.shareSave(result);
    }

//    /** TODO 评论不走这个
//     * 作品评论任务
//     */
////    @Scheduled(fixedDelay=60 * 1000)
//    public void commentOpusTask() {
//        String key = RedisKeys.BbsKey.COMMENT_ARTICLE_KEY.getKey();
//        BoundHashOperations<String, String, Object> result = getResult(key);
//        opusService.commentSave(result);
//    }

    /**
     * 文章浏览任务
     */
    @Scheduled(fixedDelay = 15 * 1000)
    public void browseArticleTask() {
        String key = RedisKeys.BbsKey.BROWSE_ARTICLE_KEY.getKey();
        BoundHashOperations<String, String, Object> result = getResult(key);
        iInteractService.browseSave(result);
    }

    /**
     * 文章点赞任务
     */
    @Scheduled(fixedDelay = 15 * 1000)
    public void likeArticleTask() {
        String key = RedisKeys.BbsKey.LIKE_ARTICLE_KEY.getKey();
        BoundHashOperations<String, String, Object> result = getResult(key);
        iInteractService.likeSave(result);
    }

    /**
     * 文章分享任务
     */
    @Scheduled(fixedDelay = 15 * 1000)
    public void shareArticleTask() {
        String key = RedisKeys.BbsKey.SHARE_ARTICLE_KEY.getKey();
        BoundHashOperations<String, String, Object> result = getResult(key);
        iInteractService.shareSave(result);
    }

    /**
     * 商品分享任务
     */
    @Scheduled(fixedDelay = 15 * 1000)
    public void shareCommonGoodsTask() {
        //普通商品
        RedisKeys.BbsKey commonGoodsKey = RedisKeys.BbsKey.SHARE_COMMON_GOODS_KEY;
        goodsShareService.shareSave(commonGoodsKey, getResult(commonGoodsKey.getKey()));

        //服务商品
        RedisKeys.BbsKey serviceGoodsKey = RedisKeys.BbsKey.SHARE_SERVICE_GOODS_KEY;
        goodsShareService.shareSave(serviceGoodsKey, getResult(serviceGoodsKey.getKey()));

        //秒杀商品
        RedisKeys.BbsKey seckillGoodsKey = RedisKeys.BbsKey.SHARE_SECKILL_GOODS_KEY;
        goodsShareService.shareSave(seckillGoodsKey, getResult(seckillGoodsKey.getKey()));

        //拼团商品
        RedisKeys.BbsKey groupBuyGoodsKey = RedisKeys.BbsKey.SHARE_GROUP_BUY_GOODS_KEY;
        goodsShareService.shareSave(groupBuyGoodsKey, getResult(groupBuyGoodsKey.getKey()));
    }


    private BoundHashOperations<String, String, Object> getResult(String key) {
        return redisUtils.boundHashOps(key);
    }


}
